package datastructure.book.tujiesuanfa.arr.solution._1_easy;

import datastructure.book.tujiesuanfa.list.bean.ListNode;
import datastructure.book.tujiesuanfa.list.bean.Node;
import org.junit.jupiter.api.Test;

import java.util.*;

public class Solution1 {
    /**
     * 将所有奇数调整至偶数之前，可以不考虑顺序
     * 示例 1：
     * 输入：actions = [1,2,3,4,5]
     * 输出：[1,3,5,2,4]或者[1,5,3,2,4]
     */
    public int[] trainingPlan(int[] actions) {
        int slow = 0;
        for (int fast = 0; fast < actions.length; fast++) {
            if (actions[fast] % 2 != 0) {
                if (slow != fast) {
                    int temp = actions[fast];
                    actions[fast] = actions[slow];
                    actions[slow] = temp;
                }
                slow++;
            }
        }
        return actions;
    }

    @Test
    public void test() {
        int[][] ints = fileCombination(18);
        System.out.println(Arrays.deepToString(ints));
    }

    /**
     * 连续正整数（至少2个）总和为指定数字 target 的所有组合
     * 例1：
     * 输入：target = 12
     * 输出：[[3, 4, 5]]
     * 例2：
     * 输入：target = 18
     * 输出：[[3,4,5,6],[5,6,7]]
     */
    public int[][] fileCombination(int target) {
        int slow = 1,fast = 2;
        LinkedList<int[]> ints = new LinkedList<>();
        while (!((fast - slow) == 1 && (slow + fast) > target)) {
            int count = 0;
            for (int i = slow; i <= fast; i++) {
                count+=i;
            }
            if (count < target) {
                fast++;
            } else if (count > target) {
                slow++;
            } else {
                int[] ints1 = new int[fast - slow + 1];
                for (int i = slow,j=0; i <= fast; i++,j++) {
                    ints1[j] = i;
                }
                ints.add(ints1);
                slow++;fast++;
            }
        }
        int[][] result = new int[ints.size()][];
        for (int i1 = 0; i1 < result.length; i1++) {
            result[i1] = ints.get(i1);
        }
        return result;
    }
    @Test
    public void test2() {
        int[] ints = statisticalResult(new int[]{2,4,6,8,10});
        System.out.println(Arrays.toString(ints));
    }

    /**
     * 返回数组arrayB，arrayB[i] = arrayA除去第i个元素其余元素的乘积
     * arrayA中有可能有0
     * 例如：
     * 输入：arrayA = [2, 4, 6, 8, 10]
     * 输出：[1920, 960, 640, 480, 384]
     */
    public int[] statisticalResult(int[] arrayA) {
        if (arrayA == null || arrayA.length == 0) {
            return new int[0];
        }
        int count = 1;
        int zeroCount = 0;
        int zeroIndex = -1;
        for (int i = 0; i < arrayA.length; i++) {
            if (arrayA[i] == 0) {
                zeroCount++;
                if (zeroCount > 1) {
                    return new int[arrayA.length];
                }
                zeroIndex = i;
                continue;
            }
            count *= arrayA[i];
        }
        if (zeroCount == 1) {
            int[] ints = new int[arrayA.length];
            ints[zeroIndex] = count;
            return ints;
        }
        int[] ints = new int[arrayA.length];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = count/arrayA[i];
        }
        return ints;
    }

    /**
     * 返回数组中出现2次及以上次数的任意一元素
     * 数组中元素值小于数组长度大于等于0
     * 示例 1：
     * 输入：documents = [2, 5, 3, 0, 5, 0]
     * 输出：0 或 5
     */
    public int findRepeatDocument(int[] documents) {
        HashSet<Integer> set = new HashSet<>();
        for (int document : documents) {
            if (!set.add(document)) {
                return document;
            }
        }
        return -1;
    }

    @Test
    public void test3(){
        int[][] array = {
                {1,2,3,4},
                {12,13,14,5},
                {11,16,15,6},
                {10,9,8,7},
        };
        System.out.println(Arrays.toString(spiralArray(array)));
    }

    /**
     * 螺旋遍历数组：从左上角开始，按照 向右、向下、向左、向上 的顺序 依次 提取元素，
     * 然后再进入内部一层重复相同的步骤，直到提取完所有元素。
     * 示例 1：
     * 输入：array = [
     *          [1,2,3],
     *          [8,9,4],
     *          [7,6,5]]
     * 输出：[1,2,3,4,5,6,7,8,9]
     */
    public int[] spiralArray(int[][] array) {
        if (array == null || array.length == 0) {
            return new int[0];
        }
        int i = array.length;
        int j  =array[0].length;
        int[] result = new int[i * j];
        int dir = 0;
        int low = 0;
        int line = 0;
        for (int k = 0; k < result.length;) {
            switch (dir) {
                case 0:
                    for (int l = 0; l < j; l++,k++) {
                        result[k] = array[low][line];
                        line++;
                    }
                    line--;
                    i--;
                    low++;
                    dir = 1;
                    break;
                case 1:
                    for (int l = 0; l < i; l++,k++) {
                        result[k] = array[low][line];
                        low++;
                    }
                    low--;
                    j--;
                    line--;
                    dir = 2;
                    break;
                case 2:
                    for (int l = 0; l < j; l++,k++) {
                        result[k] = array[low][line];
                        line--;
                    }
                    line++;
                    i--;
                    low--;
                    dir = 3;
                    break;
                case 3:
                    for (int l = 0; l < i; l++,k++) {
                        result[k] = array[low][line];
                        low--;
                    }
                    low++;
                    j--;
                    line++;
                    dir = 0;
                    break;
            }
        }
        return result;
    }
}
